package com.hnkywl.sts.dao.sys;

import com.hnkywl.sts.entity.sys.Gw;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.stereotype.Repository;

/**
 * 说明：
 * 包:        com.hnkywl.sts.dao.sys
 * 类描述:    岗位dao类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Repository
public class GwDao extends SimpleHibernateTemplate<Gw, Long> {

    /**
     * 查询岗位列表
     * @param gwmc     岗位名称
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Gw> findGwList(String gwmc, int pageNo, int pageSize) {
        Page<Gw> page = new Page<Gw>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("lastUpdated");
        if (StringUtils.isNotEmpty(gwmc)) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.like("mc", gwmc, MatchMode.START)});
        } else {
            return this.findAll(page);
        }
    }

    /**
     * 判断岗位属性是否重复
     * @param id       岗位id
     * @param propertyValue       岗位属性值
     * @param propertyName       岗位属性名
     * @return
     */
    public Boolean checkUnique(Long id, Object propertyValue,String propertyName) {
        String hql;
        if (propertyValue == null)
            return false;
        if(id == null){
            //修改检验
            hql = "from Gw where " + propertyName + " = ? ";
            return this.existsQuery(hql, propertyValue);
        }else{
            //新增检验
            hql = "from Gw where " + propertyName + " = ? and id <> ?";
            return this.existsQuery(hql, propertyValue,id);
        }
    }

    /**
     * 获取岗位最大排序号
     * @return
     */
    public Integer  findMaxPxh() {
        String hql = "select MAX(pxh) from Gw";
        return findInt(hql);
    }
}
